#include "script_view.hpp"
#include "app/app_base.hpp"
#include "extension/extension_manager.hpp"
#include "gui/controls/custom_controls.hpp"
#include "gui/ui_component.hpp"
#include "imgui/IconsFontAwesome4.h"
#include "imgui/imgui.h"
#include "scripting/scripting_engine.h"
#include "utils/os_utils.hpp"

unsigned int ScriptView::Flags() const 
{
    return eMenu | eDialog;
}

void ScriptView::RenderContent(ComponentFlag flag)
{
    if (flag == eMenu)
        Menu();
    else if (flag == eDialog)
        Dialog();
}

const char* ScriptView::GetName(ComponentFlag flag)
{
    if (flag == eDialog) {
        return SCRIPT_MODULE_LIST_DIALOG_NAME;
    }
    return nullptr;
}

void ScriptView::Menu()
{
    if (ImGui::BeginMenu(ICON_FA_HASHTAG " 脚本")) {
        auto fs = ImGui::GetFontSize();
        auto &items = ExtensionManager::singleton().getFunctionExtensions();
        for (auto & [x,y]: items) {
            ImGui::PushID(&x);
            auto icon = (*y)->icon();
            if (icon != nullptr) {
                ImGui::Image(icon, fs, fs);
                ImGui::SameLine();
            }
            if (ImGui::Selectable((*y)->title(), false, ImGuiSelectableFlags_SpanAllColumns|ImGuiSelectableFlags_AllowOverlap)) {
                (void)(*y)->call();
            }
            if (ImGui::IsItemHovered () && ImGui::IsMouseClicked (ImGuiMouseButton_Right))
            {
                // open script file
                DefaultAppOpen ((*y)->file ());
                ImGui::CloseCurrentPopup ();
            }
            ImGui::LongHoveredTooltips([&y, icon]{
                ImGui::AlignTextToFramePadding();
                ImGui::Image(icon, 128, 128);
                ImGui::Separator();
                ImGui::BulletText("%s", (*y)->title());
                ImGui::BulletText("%s", (*y)->file());
            }, 0.1);
            ImGui::PopID();
        }

        if (!items.empty())
            ImGui::Separator();

        if (ImGui::MenuItem(ICON_FA_CUBE " 显示模块信息"))
        {
            App->ShowDialog(SCRIPT_MODULE_LIST_DIALOG_NAME);
        }

        ImGui::EndMenu();
    }
}

void ScriptView::Dialog()
{
    ImGui::AlignTextToFramePadding();

    int count = 0;
    auto engines = ScriptEngineList(&count);
    if (count == 0)
    {
        ImGui::CenteredText(ICON_FA_ARROW_RIGHT " 当前没有可用的脚本模块 " ICON_FA_ARROW_LEFT, count);
    }
    else
    {
        for (int i = 0; i < count; ++i)
        {
            ImGui::PushID(i);
            auto engine = engines[i];

            ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(246, 230, 0, 255));
            ImGui::SeparatorText(engine->name);
            ImGui::PopStyleColor();

            ImGui::BulletText("名称: %s", engine->name);
            ImGui::BulletText("语言: %s", engine->lang);
            ImGui::BulletText("说明: %s", engine->desc);
            ImGui::BulletText("备注: %s", engine->support_hot_reload ? "支持热重载" : "不支持热重载");

            ImGui::Spacing();

            ImGui::PopID();
        }
    }
}
